package com.example.controller;

import com.example.common.AutoLog;
import com.example.common.Result;
import com.example.entity.Book;
import com.example.entity.Params;
import com.example.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: PengFei.He
 * @Date: 2024/11/6 19:59
 * @Function:
 */

@RestController
@RequestMapping("/book")
public class BookController {

    private static final Logger log = LoggerFactory.getLogger(BookController.class);

    @Autowired
    private BookService bookService;

    @GetMapping("/search")
    public Result findBySearch(Params params){
        //1.开启分页
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        //2.查询信息
        List<Book> list = bookService.findBySearch(params);
        //3.封装对象
        PageInfo<Book> info = PageInfo.of(list);
        return Result.success(info);
    }


    @PostMapping
    @AutoLog("修改图书信息")
    public Result save(@RequestBody Book book){
        bookService.add(book);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @AutoLog("删除图书信息")
    public Result del(@PathVariable Integer id){
        bookService.removeById(id);
        return Result.success();
    }


    /**
     * echarts饼图：根据图书分类统计每一类的数量，做成key-value的形式
     * @return
     */
    @GetMapping("/echarts/bie")
    public Result bie(){
        List<Map<String, Object>> list = bookService.bie();
        return Result.success(list);
    }

    /**
     * echarts柱状图和折线图一个数据结构：根据图书分类统计每一类的数量，做成key-value的形式
     * @return
     */
    @GetMapping("/echarts/bar")
    public Result bar(){
        Map<Object, Object> map = bookService.bar();
        return Result.success(map);
    }
}
